﻿using XYS.Core.Models;
using XYS.Core.Util;

namespace XYS.Service.Core.Models
{
    /// <summary>
    /// 事件总线日志实体
    /// </summary>
    public class EventLogModel
    {
        private string executor;

        /// <summary>
        /// Id
        /// </summary>
        public string Id => $"{DateTime.Now:HHmmssfff}{Guid.NewGuid()}";
        /// <summary>
        /// 事件名称
        /// </summary>
        public string EventName { get; set; }
        /// <summary>
        /// 消息体
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 尝试自动 序列化 消息体
        /// </summary>
        public Dictionary<string, object> MessageDeserialized
        {
            get
            {
                if (Message.IsNullOrWhiteSpace()) return new Dictionary<string, object>();
                try
                {
                    var map = Message.ToObject<Dictionary<string, object>>();
                    foreach (var key in map.Keys)
                    {
                        object data = map[key];
                        if (data == null) continue;
                        try
                        {
                            string json = data.ToJson();
                            map[key] = json.ToObject<Dictionary<string, object>>();
                        }
                        catch { }

                    }

                    return map;
                }
                catch { return new Dictionary<string, object>(); }
            }
        }

        /// <summary>
        /// 消费者
        /// </summary>
        public string Consumer { get => $"{GlobalCoreSetting.NACOS_IP}:{GlobalCoreSetting.NACOS_PORT}"; set => executor = value; }

        /// <summary>
        /// 触发时间
        /// </summary>
        public DateTime TriggerTime { get; set; }
    }
}
